<template>
  <div>
    <div class="info-box">
      <div class="title">
        <h2>个人信息</h2>
        <el-button type="primary" size="small" @click="handleEdit"
          >编辑信息</el-button
        >
      </div>
      <div>账号：{{ userInfo.username }}</div>
      <div>昵称：{{ userInfo.nickname }}</div>
      <div>电话：{{ userInfo.phone }}</div>
      <div>邮箱：{{ userInfo.email }}</div>
      <div>性别：{{ userInfo.gender ? "男" : "女" }}</div>
    </div>
    <el-dialog title="个人信息" :visible.sync="dialogVisible" width="40%">
      <el-form
        :model="form"
        ref="form"
        :rules="rules"
        label-width="80px"
        :inline="false"
        size="normal"
      >
        <el-form-item label="账号" prop="username">
          <el-input v-model="form.username"></el-input>
        </el-form-item>
        <el-form-item label="昵称" prop="nickname">
          <el-input v-model="form.nickname"></el-input>
        </el-form-item>
        <el-form-item label="电话" prop="phone">
          <el-input v-model="form.phone"></el-input>
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="form.email"></el-input>
        </el-form-item>
        <el-form-item label="性别" prop="gender">
          <el-switch v-model="form.gender" active-text="男" inactive-text="女">
          </el-switch>
        </el-form-item>
        <el-form-item label="头像" prop="avatar">
          
        </el-form-item>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button size="small" @click="dialogVisible = false">取 消</el-button>
        <el-button size="small" type="primary" @click="handleSure"
          >确 定</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>

<script>
import userApi from "@/api/user";
export default {
  name: "userInfo",
  data() {
    return {
      userId: null,
      userInfo: {},
      dialogVisible: false,
      form: {},
      rules: {},
    };
  },
  created() {
    this.userInfo = this.$store.getters.getUser;
    this.userId = this.userInfo.id;
  },
  methods: {
    getUserInfo() {
      userApi
        .getUser(this.userId)
        .then(({ data: res }) => {
          console.log(res);
          this.form = res.data.data;
        })
        .catch((err) => {
          console.log(err);
        });
    },
    handleEdit() {
      this.getUserInfo();
      this.dialogVisible = true;
    },
    handleSure() {
      userApi
        .updateUser(this.form)
        .then(({ data: res }) => {
          if (res.code === 200) {
            this.$message.success("编辑个人信息成功");
            this.dialogVisible = false;
          } else {
            this.$message.error(res.msg);
          }
        })
        .catch((err) => {
          console.log(err);
        });
    },
  },
};
</script>

<style lang="scss" scoped>
.info-box {
  .title {
    display: flex;
    justify-content: space-between;
    align-items: center;
  }
  div {
    margin: 10px 10px;
  }
}

</style>